home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP!!! - loop control problems
- Date: 21 Jan 1996 20:31:54 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4dt16a$7md@werple.net.au>
- References: <4ds174$6t0@bolivia.it.earthlink.net>
- NNTP-Posting-Host: werple.mira.net.au
-
- macc@earthlink.net (OB1) writes:
-
- >I have the following code in a program I'm writing:
-
- >const long SCREEN_SIZE = 320 * 200;
- >long count = 0;
- >while (count <= SCREEN_SIZE)
- >{
- > ... code (count gets incremented)
- >}
-
- >When I trace through my program, I find that the loop end condition is
- >always true and the code inside the loop is never run. I looking for
- >any ideas on why this doesn't work.
-
- Because you are multiplying two 'int' types, and the result has
- overflowed the capacity of an 'int' to give a negative number. The
- negative number is then converted to 'long'. For a compiler using 32-bit
- 'int's this would have worked; your compiler is probably using 16 bits.
- (The highest 16-bit integer is 32767; 320*200 is 64000).
-
- Try this instead:
-
- const long SCREEN_SIZE = 320L * 200;
-
- David White
- davidw@werple.mira.net.au
-